In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.
Trees occur very often in computer science. As opposed to trees in nature, computer science trees "grow upside down"; the root is up and the leaves are down.
A tree consists of elements named nodes. A root is one of the nodes. Each node (except for the root) has its (exactly one) father . If the node is the father of , then is a son of . Nodes that have no sons are called leaves. Sons of the node , their sons, sons of their sons, and so on, are called descendants of the node . Every node - except for the root - is a descendant of the root.
Each node has a level number assigned to it. The level number of the root is , and the level number of sons is greater by then that of their father.
A tree is a complete binary tree if and only if each node has exactly two or zero sons. In binary trees sons are named left and right.
The following picture shows an example of a complete binary tree. The nodes of that tree are numbered in a special order called preorder. In this order the root has the number , a father precedes its sons, and the left son and any its descendant have smaller numbers than the right son and every its descendant.
There are many written representations of complete binary trees having such numbering of nodes. Three ones follow.
Genealogical representation.
It is a sequence of numbers. The first element of the sequence equals
(zero), and for , the -th element of
the sequence is the number of the father of the node .
Bracket representation.
Each node corresponds to a string composed of brackets. Leaves correspond
to
. Each other node corresponds to a string , where and denote the strings that left and
right sons of respectively correspond to. The string the root
corresponds to is the bracket representation of the tree.
Level representation.
It is a sequence of level numbers of successive tree leaves (according to
the
assumed numbering).
The tree in the picture may be described as follows:
Genealogical representation | 0 1 2 2 4 4 1 7 7 |
Bracket representation | ((()(()()))(()())) |
Level representation | 2 3 3 2 2 |
Write a program that reads from the standard input a sequence of numbers and examines whether it is the level representation of a complete binary tree. If not, the program writes one word NIE ("") in the standard output. If so, the program finds two other representations of this tree (genealogical and bracket ones), and writes them in the standard output.
In the first line of the standard input there is a positive number of the sequence elements (not greater than 2500). In the second line there are successive elements of the sequence separated by single spaces.
The numbers in the standard input are written correctly. Your program need not verify that.
The standard output should contain:
For the input data:
5 2 2 3 3 2
the correct result is:
0 1 2 2 1 5 6 6 5 ((()())((()())()))
For the input data:
4 1 2 2 3
the correct result is:
NIE
Task author: Wojciech Rytter.